package com.longkey.workflow.client.view
{
	import com.longkey.workflow.client.constants.ElementType;
	import com.longkey.workflow.client.control.Context;
	import com.longkey.workflow.client.event.PropertyEvent;
	import com.longkey.workflow.client.property.PropertyEditor;
	
	import spark.components.Group;
	
	[Event(name="openPropEditor", type="com.longkey.workflow.client.event.PropertyEvent")]
	public class PropertyView extends Group
	{
		private var templateEditor:PropertyEditor;
		private var startEditor:PropertyEditor;
		private var taskEditor:PropertyEditor;
		private var procEditor:PropertyEditor;
		private var endEditor:PropertyEditor;
		private var linkEditor:PropertyEditor;
		private var currentEditor:Object;
		
		public function PropertyView()
		{
			super();
			Context.propView = this;
			init();
		}
		
		private function init():void{
			this.templateEditor = new PropertyEditor(ElementType.TEMPLATE);
			this.startEditor = new PropertyEditor(ElementType.START);
			this.taskEditor = new PropertyEditor(ElementType.TASK);
			this.procEditor = new PropertyEditor(ElementType.PROC);
			this.endEditor = new PropertyEditor(ElementType.END);
			this.linkEditor = new PropertyEditor(ElementType.LINK);
			
			this.addEventListener("openPropEditor",openPropEditor);
		}
		
		public function openPropEditor(event:PropertyEvent):void{
			var selectedEditor:PropertyEditor = null;
			switch(event.elementType){
				case ElementType.START:
					selectedEditor = this.startEditor;
					break;
				case ElementType.TASK:
					selectedEditor = this.taskEditor;
					break;
				case ElementType.PROC:
					selectedEditor = this.procEditor;
					break;
				case ElementType.END:
					selectedEditor = this.endEditor;
					break;
				case ElementType.LINK:
					selectedEditor = this.linkEditor;
					break;
				case ElementType.TEMPLATE:
					selectedEditor = this.templateEditor;
					break;
				default:
					break;
			}
			if(selectedEditor == null)
				return;
			if(this.currentEditor != null){
				this.removeElementAt(0);
			}
			if(event.elementId != selectedEditor.elementId){
				selectedEditor.elementId = event.elementId;
				selectedEditor.dataProvider = event.owner;
			}
			this.addElement(selectedEditor);
			this.currentEditor = selectedEditor;
		}
	}
}